﻿using System.Security.Cryptography;
using System.Text;

namespace RedPacket.Utils;

/// <summary>
/// sha256 秘钥加密
/// </summary>
public static class Sha256WithRsa
{
    /// <summary>
    /// sha256加密
    /// </summary>
    /// <param name="data"></param>
    /// <param name="privateKey"></param>
    /// <returns></returns>
    public static string ToSha256WithRsa(this string data, string privateKey)
    {
        using RSA rSA = RSA.Create();

        rSA.ImportPkcs8PrivateKey(Convert.FromBase64String(privateKey), out var _);

        return Convert.ToBase64String(rSA.SignData(Encoding.UTF8.GetBytes(data), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1));
    }
}

